package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.mapper.OrderItemMapper;
import com.situ.mall.mapper.OrderMapper;
import com.situ.mall.pojo.Order;
import com.situ.mall.pojo.OrderItem;
import com.situ.mall.pojo.query.OrderQuery;
import com.situ.mall.pojo.vo.OrderUserVO;
import com.situ.mall.service.IOrderService;
import com.situ.mall.util.LayUITableResult;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public LayUITableResult selectByPage(OrderQuery orderQuery) {
        PageHelper.startPage(orderQuery.getPage(), orderQuery.getLimit());
        List<OrderUserVO> list = orderMapper.selectAll(orderQuery);
//        System.out.println(list);
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        return LayUITableResult.ok((int) total, list);
    }

    @Override
    public boolean deleteById(Long orderNo) {
        return orderMapper.deleteByPrimaryKey(orderNo) > 0;
    }

    @Override
    public boolean deleteAll(Long[] orderNoes) {
        int count = 0;
        for (Long orderNo : orderNoes) {
            count += orderMapper.deleteByPrimaryKey(orderNo);
        }
        return count == orderNoes.length;
    }

    @Override
    public LayUITableResult checkByOrderNo(OrderQuery orderQuery) {
        PageHelper.startPage(orderQuery.getPage(), orderQuery.getLimit());
        List<OrderItem> list = orderItemMapper.checkByOrderNo(orderQuery.getOrderNo());
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        return LayUITableResult.ok((int) total, list);
    }

    @Override
    public boolean updatePostage(Long orderNo, Integer postage) {
        Order order = new Order();
        order.setOrderNo(orderNo);
        order.setPostage(postage);
        return orderMapper.updateByPrimaryKeySelective(order) > 0;
    }
}
